//
//  UIImage+JMIP.m
//  ImagePicker
//
//  Created by Jamin on 2018/10/16.
//  Copyright © 2018 jamin. All rights reserved.
//

#import "UIImage+JMIP.h"

@implementation UIImage (JMIP)

- (UIImage *)jmip_imageResize:(CGSize)size {
//    if (self.size.width <= size.width && self.size.height <= size.height) {
//        return self;
//    }
    if (CGSizeEqualToSize(self.size, size)) {
        return self;
    }
    CGFloat width = CGImageGetWidth(self.CGImage);
    CGFloat height = CGImageGetHeight(self.CGImage);
    float heightRadio = size.height / height;
    float widthRadio = size.width / width;
    
    float radio = MIN(heightRadio, widthRadio);
    width = floorf(width * radio);
    height = floorf(height * radio);
    
     UIGraphicsBeginImageContextWithOptions((CGSize){width, height}, NO, 0);
    [self drawInRect:(CGRect){CGPointZero, size}];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

@end
